package org.framework.app.model.svc;

import net.pgia.pub.util.UtilException;
import net.pgia.pub.util.common.ObjectUtil;
import net.pgia.pub.util.common.StringUtil;

import org.apache.log4j.Logger;
import org.framework.app.exception.AppException;
import org.framework.app.exception.SysException;



/**
 * 
 * Title: 信息分析系统<br>
 * Description: <br>
 * Date: 2012-08-16 <br>
 * Copyright (c) 2011 PGIASoft <br>
 *
 * @author zdp
 */
public class SVCFactory {
	private static Logger log = Logger.getLogger(SVCFactory.class);

	private static SVCFactory instance;

	private static final String SERVICE_IMPL_SUFFIX = "Impl";

	private static final String SEPARATOR = ".";
	

	/**
	 * 获取Service工厂实例，没有则创建新实例
	 * 
	 * @param path
	 * @return
	 * @throws SysException
	 * @throws AppException
	 */
	synchronized public static SVCFactory initServiceFactory() throws AppException, SysException {
		if (null == instance) {
			instance = new SVCFactory();
		}
		return instance;
	}

	/**
	 * 通过Service接口类获取对应的Service实现
	 * 
	 * @param ServiceClass
	 * @return
	 * @throws AppException
	 * @throws SysException
	 */
	public static ISVC getService(Class ServiceClass) throws AppException, SysException {
		if (null == ServiceClass) {
			throw new AppException("", "无效的参数Null！");
		}
		if (null == instance) {
			initServiceFactory();
		}
		ISVC serviceImpl = null;
		log.debug(ServiceClass.getPackage().getName());
		log.debug(ServiceClass.getName());
		StringBuffer className = new StringBuffer(ServiceClass.getName());
		int i = className.lastIndexOf(SEPARATOR);

		className.append(SERVICE_IMPL_SUFFIX);
		className.deleteCharAt(i + 1);
		//TODO 不同接口的service，待实现，目前先写死走beanservicen
		// className.insert(i, "beanservice");
		
		String imVision = "";
		if (StringUtil.isBlank(imVision)) {
			//throw new AppException("", "缺少SYS_CONFIG_ID=82010系统配置，请配置的本地化系统外围接口参数！");
		}else{
			className.insert(i, imVision);
		}
		//className.insert(i,SEPARATOR);
		
		String ServiceImplName = className.toString();
		log.debug(className);
		try {
			serviceImpl = (ISVC) ObjectUtil.createByName(ServiceImplName);
		} catch (UtilException e) {
			log.error(ServiceImplName + e.getMessage());
			throw new SysException("", ServiceImplName + "实例化错误！",e);
		}
		return serviceImpl;
	}
	/**
	 * 获取Debug模式下的delegate实现类 
	 * Debug模式下，直接通过Delegate接口类获取对应的Bean接口调用方式的Delegate实现
	 * 
	 * @param DelegateClass
	 * @return
	 * @throws AppException
	 * @throws SysException
	 */
	public static ISVC getServiceDebugMode(Class iServiceClass) throws AppException, SysException {
		
		ISVC serviceImpl = null;
		log.debug(iServiceClass.getPackage().getName());
		log.debug(iServiceClass.getName());
		StringBuffer className = new StringBuffer(iServiceClass.getName());
		int i = className.lastIndexOf(SEPARATOR);

		
		className.append(SERVICE_IMPL_SUFFIX);
		className.deleteCharAt(i + 1);
		//TODO 不同接口的delegate，待实现，目前先写死走beanimpl
		className.insert(i, SERVICE_IMPL_SUFFIX);    	
		className.insert(i,SEPARATOR);
		
		String ServiceImplName = className.toString();
		log.debug(className);
		try {
			serviceImpl = (ISVC) ObjectUtil.createByName(ServiceImplName);
		} catch (UtilException e) {
			log.error(ServiceImplName + e.getMessage());
			throw new SysException("", ServiceImplName + "实例化错误！",e);
		}
		return serviceImpl;
	}
	public static void main(String[] args) throws Exception {

		//ServiceFactory.getService(ISMRegionService.class);

	}
}
